package com.jnevision.deptfeemultiple.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.jnevision.deptfeemultiple.model.DeptFeeMultiple;
import com.jnevision.deptfeemultiple.service.DeptFeeMultipleServiceImpl;

/**
 * <p>
 * 加班费倍数 前端控制器
 * </p>
 *
 * @author gavin
 * @since 2019-07-26
 */

@RestController
@RequestMapping("/deptfeemultiples")
public class DeptFeeMultipleController extends ApiController {
    @Autowired
    private DeptFeeMultipleServiceImpl deptFeeMultipleServiceImpl;

    @PostMapping
    public R<Boolean> add(@RequestBody DeptFeeMultiple feemultiple) {
        return this.success(this.deptFeeMultipleServiceImpl.save(feemultiple));
    }
    
    @PostMapping("/list")
    public R<Boolean> add(@RequestBody List<DeptFeeMultiple> feemultiple) {
    	
    	List<Integer> collect = feemultiple.stream().map(DeptFeeMultiple::getDeptId).collect(Collectors.toList());
    	List<String> collect2 = feemultiple.stream().map(DeptFeeMultiple::getOvertimeDate).collect(Collectors.toList());
    	
    	if (CollectionUtils.isNotEmpty(collect)&&CollectionUtils.isNotEmpty(collect2)) {
    		deptFeeMultipleServiceImpl.remove(new QueryWrapper<DeptFeeMultiple>().in("overtime_date", collect2).in("dept_id", collect));
    	}
    	
        return this.success(this.deptFeeMultipleServiceImpl.saveBatch(feemultiple));
    }
    

    @PutMapping
    public R<Boolean> update(@RequestBody DeptFeeMultiple feemultiple) {
        return this.success(this.deptFeeMultipleServiceImpl.update(feemultiple, new UpdateWrapper<DeptFeeMultiple>()
            .eq("dept_id", feemultiple.getDeptId()).eq("overtime_date", feemultiple.getOvertimeDate())));
    }

    @GetMapping
    public R<List<DeptFeeMultiple>> get(int deptId,String overtimeDate) {
        return this
            .success(this.deptFeeMultipleServiceImpl.list(new QueryWrapper<DeptFeeMultiple>().eq("dept_id", deptId).like("overtime_date", overtimeDate)));
    }
}
